function calc_act(ExpID,adata_dir,dvh)

ud=get(dvh.hf,'UserData');
rdata_dir=ud.rdata_dir;
tmp_data_dir=ud.tmp_data_dir;

% find the adata file
[adata_file,mouse_id,userID]=find_adata_file(ExpID,adata_dir);

if isempty(adata_file)
    disp('Found no Adata file - register data and select ROIs first');
end

if ~strcmp('Adata',adata_file(1:5))
    disp('This is probably a Z stack - aborting calc act.')
    return;
end

% load the analyzed data
load([adata_dir '\' userID '\' mouse_id '\' adata_file]);

if isa(ROIs,'cell')
    if length(ROIs{1})==1
        disp('No ROIs selected - select ROIs before calculating activity')
        return;
    elseif isfield(ROIs{1},'activity')
        disp('ROI activity already calculated');
        return;
    end
else
    if length(ROIs)==1
        disp('No ROIs selected - select ROIs before calculating activity')
        return;
    elseif isfield(ROIs,'activity')
        disp('ROI activity already calculated');
        return;
    end
end

try
    nbr_piezo_layers=readini([rdata_dir userID '\' mouse_id '\' fnames{1}(1:end-3) 'ini'],'piezo.nbrlayers');
catch
    nbr_piezo_layers=1;
    disp('ATTENTION - could not read nbr of piezo layers from ini file')
end


% load the 2P data
% pre-allocate space for data
if nbr_piezo_layers>1
    data=cell(1,nbr_piezo_layers);
    for knd=1:nbr_piezo_layers
        data{knd}=zeros(size(template{1},1),size(template{1},2),length(dx{knd}),'int16');
    end
else
    data=zeros(size(template,1),size(template,2),length(dx),'int16');
end


% load the data
for ind=1:length(fnames)
    if exist([tmp_data_dir userID '\' mouse_id '\' fnames{ind}],'file');
        disp(['loading file ' fnames{ind} ' from local tmpdata directory']);
        curr_load_path=[tmp_data_dir userID '\' mouse_id '\' fnames{ind}];
    else
        disp(['loading file ' fnames{ind} ' from the network RawData directory']);
        curr_load_path=[rdata_dir userID '\' mouse_id '\' fnames{ind}];
    end
    
    fi=fopen(curr_load_path,'r');
    x_res=fread(fi,1,'int16=>double');
    y_res=fread(fi,1,'int16=>double');
    
    if nbr_piezo_layers>1
        for knd=sum(nbr_frames(1:ind-1))+1:sum(nbr_frames(1:ind))
            data{rem(knd-1,nbr_piezo_layers)+1}(:,:,floor((knd-1)/nbr_piezo_layers)+1)=reshape(fread(fi,y_res*x_res,'int16=>int16'),y_res,x_res)';
        end
    else
        for knd=sum(nbr_frames(1:ind-1))+1:sum(nbr_frames(1:ind))
            data(:,:,knd)=reshape(fread(fi,y_res*x_res,'int16=>int16'),y_res,x_res)';
        end
    end
    fclose(fi);
end

disp(['Now registering data on dx dy values and correcting line shift']);
if ~isa(dx,'cell')
    data=shift_data(data,dx,dy);
    data=correct_line_shift(data,mean(data,3));
else
    for rnd=1:length(dx)
        data{rnd}=shift_data(data{rnd},dx{rnd},dy{rnd});
        data{rnd}=correct_line_shift(data{rnd},mean(data{rnd},3));
    end
end

disp('Calculating activity');
if isa(ROIs,'cell')
    for qnd=1:length(ROIs)
        ROIs_act=ROI_activity(data{qnd},ROIs{qnd});
        ROIs{qnd}=ROIs_act;
    end
else
    ROIs=ROI_activity(data,ROIs);
end
save([adata_dir '\' userID '\' mouse_id '\Adata-' fnames{1}(1:strfind(fnames{1},'_')-1)],'ROIs','bv','np','-append');

disp('----Done calculating activity of Exp!----');
%EOF